package book.nettyinaction.ch06;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

/**
 * 代码清单 6-13 添加 ChannelFutureListener 到 ChannelFuture
 *
 * 添加 ChannelFutureListener, 它将打印栈跟踪信息并且随后关闭 Channel
 *
 */
public class Demo13ExceptionCaught {

    public static void main(String[] args) {
        Channel channel = null;
        Object someMessage = null;
        ChannelFuture future = channel.write(someMessage);

        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture f) throws Exception {
                if ( !f.isSuccess() ) {
                    f.cause().printStackTrace();
                    f.channel().close();
                }

            }
        });
    }
}
